#include "common.hpp"
#include "server_TCP.hpp"
#include "protocol.hpp"
#include "calculate.hpp"
#include "daemon.hpp"




int main(int argc, char* argv[])
{
    //./server_UDP [server_port] [server_ip] //不用传ip, 因为INADDR_ANY
    //./server_UDP [server_port]
    if(argc != 2)
    {
        std::cout << "usage: " << argv[0] << " [server_port]" << std::endl;
        exit(USAGE_ERR);
    }

    //::daemon(0, 0); //系统调用
    Daemon(0, 0); //自己造的轮子

    LOG_SWITCH_FILE;

    uint16_t port = static_cast<uint16_t>(std::stoi(argv[1]));

    //1,2,3统一为应用层

    //1.应用层 -- 针对特定应用的实现
    std::unique_ptr<calculate> cal_p = std::make_unique<calculate>();

    //2.表示层 -- 数据格式的转换
    std::unique_ptr<protocol> ptl_p = std::make_unique<protocol>([&cal_p](const request& req)-> response
                                                                 {
                                                                     return cal_p->execute(req);
                                                                 }
                                                                );

    //3.对话层 -- 建立和断开连接
    std::unique_ptr<server_tcp> st_p = std::make_unique<server_tcp>(port, [&ptl_p](std::shared_ptr<Socket>& sock_p, const Inet_Addr& client)
                                                                          {
                                                                               ptl_p->receive_request_and_send_response(sock_p, client);
                                                                          }
                                                                   );

    
    st_p->start();


    return 0;
}